#include "stdafx.h"
#include "CScriptEngine.h"

#define CMD_RESULT_BREAK			0	
#define CMD_RESULT_CONTINUE		1

CConsoleScripts			ConsoleScripts;
CScriptEngine			ScriptEngine;

int WAIT(CConsoleScript * pScript)
{
	return CMD_RESULT_BREAK;
}
int PRINT(CConsoleScript * pScript)
{
	Print(pScript->ReadString());
	return CMD_RESULT_CONTINUE;
}
int PRINTF(CConsoleScript * pScript)
{
	return CMD_RESULT_CONTINUE;
}

void CConsoleScript::Process()
{
	try
	{
		do
		{
			DWORD dwOpcode = Read<DWORD>();
		}
		while(true);
	}
	catch(const char *e)
	{
		char szError[512];
		szncpy(szError, e, sizeof(szError));
		THROW("Script Parse Error ('%s'): %s", m_pSourceFile->GetFileName());
	}
}
void CConsoleScript::ReadScript()
{
	if(auto pFile = new CFile(m_pCompiledFile->GetFilePath(), FILE_MODE_READ))
	{
		int size = pFile->GetSize();
		m_pCurrentData = m_pData = new char[size];
		pFile->Read(m_pData, size);
		delete pFile;
	}
}

void CScriptEngine::CompileScript(CFileData * pFileData)
{
	char szName[32];
	auto pDir = pFileData->GetDirectory();
	auto pScript = ConsoleScripts.Request(pFileData->GetName(szName, sizeof(szName)));
	pScript->SetSourceData(pFileData);
	m_Scripts.Add(pScript);
}